import time
import threading
import math
class WorkerProstiBrojevi():
    def __init__(self):
        self.start =0
        self.end =0
        self.chunkId =-1
        self.working = False
        self.running = False
        self.prosti = []
       
        
    def threadMethod(self):
        self.running = True
        while self.running == True:
            while (self.start == 0 or self.end == 0) and self.running == True:
                try:
                    time.sleep(1)
                except:
                    print "Ja vise nikada necu da spavam"
            
            if self.running == True:
                print "Krenuo sam da trazim proste " + str(self.start) + " do " + str(self.end)
                self.working = True
                for x in range(self.start, self.end):
                    time.sleep(0.00001)
                    if self.prost(x) == True:
                        self.prosti.append(x)
                print "Prestao sam da radim " + str(self.start) + " do " + str(self.end)
                print self.prosti
                self.start =0
                self.end =0
                self.working = False
            
    def prost(self,n): 
        if n % 2 == 0 and n > 2: 
            return False
        return all(n % i for i in range(3, int(math.sqrt(n)) + 1, 2))
    '''for a in range(2,num):#1725227 ovo govno ce vratiti da je prost a ovaj nije a za male brojeve vraca dobro
            if num % a ==0:
                return False
            else:
                return True'''
    
    def startJob(self):
        t = threading.Thread(target=self.threadMethod, args=())
        t.daemon = True
        t.start()
        
    def setChunk(self, chunkId, start, end):
        #print "WORKER ", start, " " , end
        self.working  = True
        self.chunkId = chunkId
        self.start = start
        self.end = end
    
    
    
    